import {call, put, takeEvery, all} from 'redux-saga/effects'
import {JSONP} from '../../utils'
import {CHINA_TOTAL, CHINA_ADD, TOTAL_DATA, AREA_TREE} from '../types'
// saga generator函数
function* fetchTotalDataSaga(){
    let res = yield call(JSONP, `https://view.inews.qq.com/g2/getOnsInfo?name=disease_h5&_=1580698876310`, 'totalData')

    yield put({ // 替代dispatch的副作用
        type: AREA_TREE,
        payload: res.areaTree
    })

    yield put({ // 替代dispatch的副作用
        type: CHINA_TOTAL,
        payload: res.chinaTotal
    })

    yield put({ // 替代dispatch的副作用
        type: CHINA_ADD,
        payload: res.chinaAdd
    })

    yield put({ // 替代dispatch的副作用
        type: TOTAL_DATA,
        payload: {
            chinaDayList:res.chinaDayList,
            chinaDayAddList: res.chinaDayAddList,
            dailyNewAddHistory: res.dailyNewAddHistory,
            dailyDeadRateHistory: res.dailyDeadRateHistory,
            areaTree: res.areaTree,
            articleList: res.articleList
        }
    })
}

function* watchTotalDataAction(){
    yield takeEvery('FETCH_TOTAL_DATA', fetchTotalDataSaga)
}

function* rootSaga(){
    yield all([ 
        watchTotalDataAction()
    ])
}

export default rootSaga